﻿/***
* 暴露ask方法 用于提示用户接下去的操作
***/
define(['jquery'], function ($) {
    var module = {
        ask: function (title, content, menus, options) {
            if (!title) throw '请输入询问标题';

            options = $.extend(options || {}, this._buildContents(menus));

            options = $.extend({
                title: title,
                content: content || '',
                closeBtn: false
            }, options);

            layer.open(options);
        },
        model: {
            link: null,
            text: '',
            callback: null,
            notClose: false
        },
        _buildContents: function (options) {
            if (!options) return {};
            var btns = []; // 选项按钮
            var callbacks = {};//回调函数

            $.each(options, function (idx, option) {
                btns.push(option.text);

                if (option.link) {
                    callbacks['btn' + (idx + 1)] = function () {
                        location.href = option.link;
                    }
                } else {
                    callbacks['btn' + (idx + 1)] = function (index) {
                        if (!option.notClose) layer.close(index);
                        if (typeof (option.callback) === 'function') option.callback();
                    }
                }
            });
            return $.extend({ btn: btns }, callbacks);
        }
    };

    return module;
});